package com.dlxs.studyproject.ui.home;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.dlxs.studyproject.R;

import devin.com.picturepicker.activity.PictureGridActivity;
import devin.com.picturepicker.activity.PicturePreviewActivity;
import devin.com.picturepicker.javabean.PictureItem;
import devin.com.picturepicker.options.PickOptions;
import devin.com.picturepicker.pick.PicturePicker;

public class OnlineReportActivity extends AppCompatActivity {


    public static final int IMG_MAX_COUNT = 9;

    private static final int PICK_IMG_REQUEST = 1;

    private TextView tvCarName;
    private TextView tvCarOwner;
    private TextView tvReporter,tv_num;
    private EditText etContent;
    private RecyclerView rvImg;
    private TextView tvSubmit;

    private List<String> imgPaths = new ArrayList<>();
    private ReleaseImgAdapter releaseImgAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_online_report);
        initView();
        setListener();
        initRV();
    }

    public static void startOnlineReportActivity(Activity activity) {
        activity.startActivity(new Intent(activity, OnlineReportActivity.class));
    }


    private void initView() {
        tvCarName = findViewById(R.id.tv_car_name);
        tvCarOwner = findViewById(R.id.tv_car_owner);
        tvReporter = findViewById(R.id.tv_reporter);
        etContent = findViewById(R.id.et_content);
        tv_num = findViewById(R.id.tv_num);
        rvImg = findViewById(R.id.rv_img);
        tvSubmit = findViewById(R.id.tv_submit);



        //EditText的监听输入多少字数
        etContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @SuppressLint("SetTextI18n")
            @Override
            public void afterTextChanged(Editable editable) {
                tv_num.setText(editable.toString().length() + "/" + (300 - editable.toString()
                        .length()));
            }
        });
    }

    private void initRV() {
        releaseImgAdapter = new ReleaseImgAdapter(this, imgPaths);
        rvImg.setAdapter(releaseImgAdapter);
        releaseImgAdapter.setOnItemClickListener(new ReleaseImgAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {

                if (position < imgPaths.size()) {
                    PicturePreviewActivity.startActivityWithOnlyPreview(OnlineReportActivity.this, position, imgPaths);
                } else {
                    PickOptions options = new PickOptions.Builder()
                            .setJustTakePhoto(false)
                            .setMultiMode(true)
                            .setPickMaxCount(IMG_MAX_COUNT - imgPaths.size())
                            .setCanPreviewImg(true)
                            .setShowCamera(true)
                            .build();
                    PicturePicker.getInstance().startPickPicture(OnlineReportActivity.this, PICK_IMG_REQUEST, options);
                }
            }
        });
    }

    private void setListener() {
        tvSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s = etContent.getText().toString();
                if (TextUtils.isEmpty(s)) {
                    return;
                }
                if (imgPaths.isEmpty()) {
                    return;
                }
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        etContent.setText(null);
                        imgPaths.clear();
                        releaseImgAdapter.notifyDataSetChanged();
                    }
                }, 1500);

            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null && requestCode == PICK_IMG_REQUEST) {
            List<PictureItem> tempList = (List<PictureItem>) data.getSerializableExtra(PictureGridActivity.EXTRA_RESULT_PICK_IMAGES);
            for (PictureItem pictureItem : tempList) {
                imgPaths.add(pictureItem.pictureAbsPath);
            }
            releaseImgAdapter.notifyDataSetChanged();
        }
    }
}